<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OfficialAccountMessage.php 2018-05-24 16:39 likexin
   
*/

namespace common\library\wechat\officialaccount;

use common\component\wechat\Wechat;
use common\library\wechat\WechatApi;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Messages\Article;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\Media;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Raw;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Messages\Video;
use EasyWeChat\Kernel\Messages\Voice;

class OfficialAccountMessage extends WechatApi
{

    /**
     * 发送文本消息
     * @param string $openid 目标用户OPENID
     * @param string $text 发送文字
     * @return mixed
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendText(string $openid, string $text)
    {
        try {
        $message = new Text($text);
        $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送图片消息
     * @param string $openid 目标用户OPENID
     * @param int $mediaId 媒体资源ID
     * @return mixed
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendImage(string $openid, string $mediaId)
    {
        try {
        $message = new Image($mediaId);
        $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送视频消息
     * @param string $openid 目标用户OPENID
     * @param array $video 视频对象信息
     * @param string $video [title] 标题
     * @param string $video [description] 介绍
     * @param string $video [media_id] 视频媒体资源ID
     * @param string $video [thumb_media_id] 封面图媒体资源ID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendVideo(string $openid, array $video)
    {
        if (!isset($video['media_id']) || empty($video['media_id'])) {
            return error('media_id不能为空');
        }
        if (!isset($video['thumb_media_id']) || empty($video['thumb_media_id'])) {
            return error('thumb_media_id不能为空');
        }

        try {
        $message = new Video($video['media_id'], [
            'title' => $video['title'],
            'description' => $video['description'],
            'thumb_media_id' => $video['thumb_media_id'],
        ]);
        $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送音频消息
     * @param string $openid 目标用户OPENID
     * @param string $mediaId 媒体资源ID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendVoice(string $openid, string $mediaId)
    {
        try {
        $message = new Voice($mediaId);
        $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送图文消息
     * @param string $openid 目标用户OPENID
     * @param array $newList 图文消息列表二维数组
     * @param array $newList [][title] 标题
     * @param array $newList [][description] 描述
     * @param array $newList [][image] 图片地址
     * @param array $newList [][url] URL地址
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendNews(string $openid, array $newsList)
    {
        if (count($newsList) > 8) {
            return error('图文消息最大数量为8');
        }
        $news = [];
        foreach ($newsList as $newsRow) {
            if (empty($newsRow) || !is_array($newsRow)) {
                continue;
            }
            $news[] = new NewsItem($newsRow);
        }

        try {
        $message = new News($news);
        $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送文章消息
     * @param string $openid 目标用户OPENID
     * @param array $article 文章对象信息
     * @param string $article [title] 标题 必填
     * @param string $article [author] 作者
     * @param string $article [content] 内容
     * @param string $article [thumb_media_id] 图文消息的封面图片素材ID(必须是永久mediaId)
     * @param string $article [digest] 图文消息的摘要
     * @param string $article [source_url] 来源URL
     * @param string $article [show_cover] 是否显示封面 0不显示 1显示
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     *
     */
    public static function sendArticle(string $openid, array $article)
    {
        $article['show_cover'] = (int)$article['show_cover'];
        $message = new Article($article);

        try {
            $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (InvalidArgumentException $exception) {
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送素材消息
     * @param string $openid 目标用户OPENID
     * @param string $mediaId 素材ID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendMedia(string $openid, string $mediaId)
    {
        try {
        $message = new Media($mediaId, 'mpnews');
        $result = self::getInstance()->customer_service->message($message)->to($openid)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 发送原始消息
     * @param string $rawMessage 原始消息
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/messages
     */
    public static function sendRaw(string $rawMessage)
    {
        try {
        $message = new Raw($rawMessage);
            $result = self::getInstance()->customer_service->message($message)->send();
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

}